const db = require('../config/database');

class Product {
    static async create(productData) {
        try {
            const { name, code, category_id, price, cost, unit, stock, min_stock, status = 'active' } = productData;
            
            const [result] = await db.query(
                'INSERT INTO products (name, code, category_id, price, cost, unit, stock, min_stock, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
                [name, code, category_id, price, cost, unit, stock, min_stock, status]
            );
            
            return result.insertId;
        } catch (error) {
            console.error('创建商品失败:', error);
            throw error;
        }
    }

    static async findById(id) {
        try {
            const [rows] = await db.query(
                'SELECT * FROM products WHERE id = ?',
                [id]
            );
            return rows[0] || null;
        } catch (error) {
            console.error('查询商品失败:', error);
            throw error;
        }
    }

    static async findAll() {
        try {
            const [rows] = await db.query('SELECT * FROM products');
            return rows;
        } catch (error) {
            console.error('查询所有商品失败:', error);
            throw error;
        }
    }

    static async update(id, productData) {
        try {
            const { name, code, category_id, price, cost, unit, stock, min_stock, status } = productData;
            
            const [result] = await db.query(
                'UPDATE products SET name = ?, code = ?, category_id = ?, price = ?, cost = ?, unit = ?, stock = ?, min_stock = ?, status = ? WHERE id = ?',
                [name, code, category_id, price, cost, unit, stock, min_stock, status, id]
            );
            
            return result.affectedRows > 0;
        } catch (error) {
            console.error('更新商品失败:', error);
            throw error;
        }
    }

    static async delete(id) {
        try {
            const [result] = await db.query('DELETE FROM products WHERE id = ?', [id]);
            return result.affectedRows > 0;
        } catch (error) {
            console.error('删除商品失败:', error);
            throw error;
        }
    }

    static async findByCode(code) {
        try {
            const [rows] = await db.query(
                'SELECT * FROM products WHERE code = ?',
                [code]
            );
            return rows[0] || null;
        } catch (error) {
            console.error('查询商品编码失败:', error);
            throw error;
        }
    }

    static async checkCodeExists(code, excludeId = null) {
        try {
            let query = 'SELECT * FROM products WHERE code = ?';
            const params = [code];
            
            if (excludeId) {
                query += ' AND id != ?';
                params.push(excludeId);
            }
            
            const [rows] = await db.query(query, params);
            return rows.length > 0;
        } catch (error) {
            console.error('检查商品编码失败:', error);
            throw error;
        }
    }
}

module.exports = Product; 